<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>list page</title>
</head>
<body>
    <script>
        var websocket = null;
        if ('WebSocket' in window){
            websocket = new WebSocket("ws://");
        }else{
            alert('该浏览器不支持websocket!');
        }
        {#websocket的事件:刚刚建立的事件#}
        websocket.onopen = function (event) {
            console.log('建立连接');
        }
        websocket.onclose = function (event) {
            console.log("连接关闭");
        }
        websocket.onmessage =  function (event) {
            console.log('收到消息:'+ event.data);
            //还可以弹窗提醒,播放音乐等
        }
        websocket.onerror = function () {
            alert('websocket通信发生错误!');
        }
        // 在浏览器窗口关闭之前先关闭websocket
        window.onbeforeunload = function () {
            websocket.close();
        }


    </script>
</body>
</html>